; font4.asm
; 4x8 pixel font

	.include "common.inc"
	.include "global.inc"
	.include "graphic.inc"
	.include "petscii.inc"
	.include "text.inc"

	.export initfnt4

	.segment "RODATA"

fnt4data:	; (chars 32-127) - nibbles are doubled in each byte
			; Font by Michael Kirchner
			.byte   0,   0,   0,   0,   0,   0,   0,   0
			.byte   0,  34,  34,  34,  34,   0,  34,   0
			.byte   0,  85,  85,   0,   0,   0,   0,   0
			.byte   0,  85, 119,  85,  85, 119,  85,   0
			.byte   0,  34,  51, 102,  51, 102,  34,   0
			.byte   0,  85,  17,  34,  34,  68,  85,   0
			.byte   0, 102, 102,  51, 102, 102,  51,   0
			.byte  34,  34,   0,   0,   0,   0,   0,   0
			.byte   0,  17,  34,  34,  34,  34,  17,   0
			.byte   0,  68,  34,  34,  34,  34,  68,   0
			.byte   0,   0,   0,  85,  34,  85,   0,   0
			.byte   0,   0,   0,  34, 119,  34,   0,   0
			.byte   0,   0,   0,   0,   0,   0,  34,  68
			.byte   0,   0,   0,   0, 119,   0,   0,   0
			.byte   0,   0,   0,   0,   0,   0,  34,   0
			.byte   0,  17,  17,  34,  34,  68,  68,   0
			.byte   0, 119,  85,  85,  85,  85, 119,   0
			.byte   0,  17,  17,  17,  17,  17,  17,   0
			.byte   0, 119,  17, 119,  68,  68, 119,   0
			.byte   0, 119,  17, 119,  17,  17, 119,   0
			.byte   0,  85,  85, 119,  17,  17,  17,   0
			.byte   0, 119,  68, 119,  17,  17, 119,   0
			.byte   0, 119,  68, 119,  85,  85, 119,   0
			.byte   0, 119,  17,  17,  17,  17,  17,   0
			.byte   0, 119,  85, 119,  85,  85, 119,   0
			.byte   0, 119,  85, 119,  17,  17, 119,   0
			.byte   0,   0,   0,  34,   0,   0,  34,   0
			.byte   0,   0,   0,  34,   0,   0,  34,  68
			.byte   0,   0,  17,  34,  68,  34,  17,   0
			.byte   0,   0,   0, 119,   0, 119,   0,   0
			.byte   0,   0,  68,  34,  17,  34,  68,   0
			.byte   0,  34,  85,  17,  34,   0,  34,   0
			.byte   0,  51,  85,  85,  85,  68,  51,   0
			.byte   0,  34,  85,  85, 119,  85,  85,   0
			.byte   0, 102,  85, 102,  85,  85, 102,   0
			.byte   0,  51,  68,  68,  68,  68,  51,   0
			.byte   0, 102,  85,  85,  85,  85, 102,   0
			.byte   0, 119,  68, 102,  68,  68, 119,   0
			.byte   0, 119,  68, 102,  68,  68,  68,   0
			.byte   0,  51,  68,  68,  85,  85,  51,   0
			.byte   0,  85,  85, 119,  85,  85,  85,   0
			.byte   0, 119,  34,  34,  34,  34, 119,   0
			.byte   0,  51,  17,  17,  17,  85,  34,   0
			.byte   0,  85,  85, 102,  85,  85,  85,   0
			.byte   0,  68,  68,  68,  68,  68, 119,   0
			.byte   0,  85, 119,  85,  85,  85,  85,   0
			.byte   0, 102,  85,  85,  85,  85,  85,   0
			.byte   0,  34,  85,  85,  85,  85,  34,   0
			.byte   0, 102,  85,  85, 102,  68,  68,   0
			.byte   0,  34,  85,  85,  85, 102,  51,   0
			.byte   0, 102,  85,  85, 102,  85,  85,   0
			.byte   0,  51,  68,  34,  17,  17, 102,   0
			.byte   0, 119,  34,  34,  34,  34,  34,   0
			.byte   0,  85,  85,  85,  85,  85,  51,   0
			.byte   0,  85,  85,  85,  85,  34,  34,   0
			.byte   0,  85,  85,  85,  85, 119,  85,   0
			.byte   0,  85,  85,  34,  85,  85,  85,   0
			.byte   0,  85,  85,  85,  34,  34,  34,   0
			.byte   0, 119,  17,  34,  34,  68, 119,   0
			.byte   0,  51,  34,  34,  34,  34,  51,   0
			.byte   0,  68,  68,  34,  34,  17,  17,   0
			.byte   0, 102,  34,  34,  34,  34, 102,   0
			.byte  34,  85,   0,   0,   0,   0,   0,   0
			.byte   0,   0,   0,   0,   0,   0,   0, 255
			.byte  34,  17,   0,   0,   0,   0,   0,   0
			.byte   0,   0,   0,  51,  85,  85,  51,   0
			.byte   0,  68,  68, 102,  85,  85, 102,   0
			.byte   0,   0,   0,  51,  68,  68,  51,   0
			.byte   0,  17,  17,  51,  85,  85,  51,   0
			.byte   0,   0,   0,  34,  85, 102,  51,   0
			.byte   0,  17,  34, 119,  34,  34,  34,   0
			.byte   0,   0,   0,  51,  85,  51,  17, 102
			.byte   0,  68,  68, 102,  85,  85,  85,   0
			.byte   0,  34,   0,  34,  34,  34,  34,   0
			.byte   0,  34,   0,  34,  34,  34,  34,  68
			.byte   0,  68,  68,  85, 102,  85,  85,   0
			.byte   0,  34,  34,  34,  34,  34,  34,   0
			.byte   0,   0,   0,  85, 119,  85,  85,   0
			.byte   0,   0,   0, 102,  85,  85,  85,   0
			.byte   0,   0,   0,  34,  85,  85,  34,   0
			.byte   0,   0,   0, 102,  85, 102,  68,  68
			.byte   0,   0,   0,  51,  85,  51,  17,  17
			.byte   0,   0,   0, 102,  85,  68,  68,   0
			.byte   0,   0,   0,  51, 102,  51, 102,   0
			.byte   0,  34,  34, 119,  34,  34,  51,   0
			.byte   0,   0,   0,  85,  85,  85,  51,   0
			.byte   0,   0,   0,  85,  85,  34,  34,   0
			.byte   0,   0,   0,  85,  85, 119,  85,   0
			.byte   0,   0,   0,  85,  34,  34,  85,   0
			.byte   0,   0,   0,  85,  85,  51,  17, 102
			.byte   0,   0,   0, 119,  17,  34, 119,   0
			.byte   0,  51,  34,  68,  34,  34,  51,   0
			.byte   0,  34,  34,   0,  34,  34,  34,   0
			.byte   0, 102,  34,  17,  34,  34, 102,   0
			.byte   0,   0,   0,  85, 170,   0,   0,   0
			.byte   0,   0,   0,   0,   0,   0,   0,   0

f4init:		.word f4_font, f4_char, f4_getextent

ch4table:	.byte $20, $41, $42, $43, $44, $45, $46, $47
			.byte $48, $49, $4A, $4B, $4C, $4D, $4E, $4F
			.byte $50, $51, $52, $53, $54, $55, $56, $57
			.byte $58, $59, $5A, $3B, $3C, $3D, $3E, $3F
			.byte $00, $01, $02, $03, $04, $05, $06, $07
			.byte $08, $09, $0A, $0B, $0C, $0D, $0E, $0F
			.byte $10, $11, $12, $13, $14, $15, $16, $17
			.byte $18, $19, $1A, $1B, $1C, $1D, $1E, $1F
			.byte $40, $21, $22, $23, $24, $25, $26, $27
			.byte $28, $29, $2A, $2B, $2C, $2D, $2E, $2F
			.byte $30, $31, $32, $33, $34, $35, $36, $37
			.byte $38, $39, $3A, $5B, $5C, $5D, $5E, $5F

	.segment "DATA"
	
cur_ch:		.tag Bitmap
bm4buf:		.res 8
	ch4ptr = reg1

	.segment "CODE"

initfnt4:	; register font with text routines
			lda #2
			ldx #<f4init
			ldy #>f4init
			jsr t_regfont
			lda #<bm4buf
			sta cur_ch+Bitmap::buf_ptr
			sta f4ptr+1
			lda #>bm4buf
			sta cur_ch+Bitmap::buf_ptr+1 
			sta f4ptr+2
			rts

f4_font:	; select font (A)
			lda #8
			sta cur_ch+Bitmap::height
			lda #4
			sta cur_ch+Bitmap::width
			rts

	.segment "DATA"
	; self modifying (f4ptr)

f4_char:	; A ... char code
			and #$7F		; code := ch4table[A&$7F]
			cmp #$60
			bcc @dotbl
			lda #$3F
@dotbl:		tax
			lda ch4table, x
			sta ch4ptr		; ptr = code
			lda #0
			ldx #3			; code mul 8
@tc_mull:	asl ch4ptr
			rol a
			dex
			bne @tc_mull
			sta ch4ptr+1	; save high byte
			clc				; add base ptr
			lda #<fnt4data
			adc ch4ptr
			sta ch4ptr
			lda #>fnt4data
			adc ch4ptr+1
			sta ch4ptr+1
			ldy #7
f4loop:		lda (ch4ptr), y
			and #$F0
f4ptr:		sta $FFFF, y
			dey
			bpl f4loop
			lda #<cur_ch	; output char
			ldx #>cur_ch
			jmp g_putbm

	.segment "CODE"

f4_getextent:
			; (reg2) .. text adr (first byte len) -> X/Y text extent
			ldy #0			; get length
			lda (reg2), y
			asl				; text length * 4
			asl
			tax
			ldy #8			; height = 8
			rts
			